﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Registered_Student_Management
{
    public partial class F_NewCourse : Form
    {
        string connection_string = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=Database.mdb;Jet OLEDB:Database Password=51002201";
        public F_NewCourse()
        {
            InitializeComponent();
        }

        private void F_NewCourse_Load(object sender, EventArgs e)
        {
            GB_NewCourse.Visible = true;
            DTP_ngaykhoitaoKhoamoi.Value = DateTime.Now;
            DTP_ngaybatdauKhoamoi.Value = DateTime.Now;
            DTP_ngayketthucKhoamoi.Value = DateTime.Now;
        }

        private void BT_moKhoamoi_Click(object sender, EventArgs e)
        {
            try
            {
                if (TB_mokhoamoi.Text == "" || TB_sotiethocKhoamoi.Text == "" || TB_tenkhoamoi.Text == "" || TB_hocphiKhoamoi.Text == "")
                {
                    MessageBox.Show("Nhập Đầy Đủ Thông Tin", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cnInsert = new OleDbConnection();
                    cnInsert.ConnectionString = connection_string;

        /*            OleDbCommand commandInsertTest = new OleDbCommand();
                    commandInsertTest.Connection = cnInsert;
                    cnInsert.Open();

                    DTP_ngaybatdauKhoamoi.Format = DateTimePickerFormat.Custom;
                    DTP_ngaybatdauKhoamoi.CustomFormat = "MM/dd/yyyy";
                    this.Controls.Add(DTP_ngaybatdauKhoamoi);
                   // DateTime start_date = new DateTime(DTP_ngaybatdauKhoamoi.Value.Month, DTP_ngaybatdauKhoamoi.Value.Day, DTP_ngaybatdauKhoamoi.Value.Year);
                    string coname = TB_mokhoamoi.Text, cocode = TB_tenkhoamoi.Text;
                    // and start_date ='" + start_dateCourse + "' [cocode] = '" + cocode + "' 
                    commandInsertTest.CommandText = "select * from [course] where (start_date ='" + DTP_ngaybatdauKhoamoi.Value.Date + "')";
                    OleDbDataAdapter adapt1 = new OleDbDataAdapter(commandInsertTest);
                    DataTable data1 = new DataTable();
                    adapt1.Fill(data1);

                    if (data1.Rows.Count > 0)
                    {
                        MessageBox.Show("Khóa Học Vừa Nhập Hiện Đã Được Mở ", "Thông Báo");
                    }
                    else
                    {
         */
                        string start_dateCourse = DTP_ngaybatdauKhoamoi.Value.ToShortDateString();
                        string create_date = DTP_ngaykhoitaoKhoamoi.Value.ToShortDateString();
                        string end_date = DTP_ngayketthucKhoamoi.Value.ToShortDateString();

                        int tuition = int.Parse(TB_hocphiKhoamoi.Text.ToString());
                        int lesson_num = int.Parse(TB_sotiethocKhoamoi.Text.ToString());
                        string coname = TB_mokhoamoi.Text, cocode = TB_tenkhoamoi.Text;

                        OleDbCommand commandInsertNewCourse = new OleDbCommand();
                        commandInsertNewCourse.Connection = cnInsert;

                        //insert new course to database
                        cnInsert.Open();

                        commandInsertNewCourse.CommandText = "insert into course (coname, cocode, tuition, start_date, end_date, create_date, lesson_num) values('" + coname + "','" + cocode + "','" + tuition + "','" + start_dateCourse + "','" + end_date + "','" + create_date + "','" + lesson_num + "')";
                        commandInsertNewCourse.ExecuteNonQuery();

                        OleDbCommand commandgetIDCourse = new OleDbCommand();
                        commandgetIDCourse.Connection = cnInsert;
                        commandgetIDCourse.CommandText = "select * from course where cocode = @cocode";
                        commandgetIDCourse.Parameters.AddWithValue("@cocode", cocode);
                        OleDbDataAdapter adapt = new OleDbDataAdapter(commandgetIDCourse);
                        DataTable data = new DataTable();
                        adapt.Fill(data);
                        DataRow dr = data.Rows[0];
                        int idCourse = int.Parse(dr["id"].ToString());
                        int waiting_student = int.Parse(dr["waiting_student"].ToString());
                        int available = int.Parse(dr["available"].ToString());

                        cnInsert.Close();
                        cnInsert.Dispose();
                        cnInsert = null;

                        MessageBox.Show("Tạo Khóa Học Thành Công", "Thông Báo");
                        Close();
        //            }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }


    }
}
